package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.NewsAuthDto;
import com.heima.model.wemedia.dtos.NewsAuthOneDto;
import com.heima.model.wemedia.dtos.NewsAuthPageDto;
import com.heima.model.wemedia.pojos.WmNews;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.wemedia.mapper.WmNewsMapper;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.ManualReviewScanService;
import com.heima.wemedia.service.WmNewsAutoScanService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Transactional
public class ManualReviewScanServiceImpl implements ManualReviewScanService {

    @Autowired
    private WmNewsMapper wmNewsMapper;

    @Autowired
    private WmUserMapper wmUserMapper;


    @Autowired
    private WmNewsAutoScanService wmNewsAutoScanService;

    private static final Integer MAXSIZE =  10;

    /**
     * 获取所有文章列表
     *
     * @param newsAuthDto
     * @return
     */
    @Override
    public ResponseResult listVo(NewsAuthDto newsAuthDto) {
        //校验size数据
        if (newsAuthDto.getSize()==null||newsAuthDto.getSize()>10||newsAuthDto.getSize()==0){
            //如果数据有问题，设置为最大值10
            newsAuthDto.setSize(MAXSIZE);
        }
        //校验page数据
        if (newsAuthDto.getPage()==null||newsAuthDto.getPage()==0){
            newsAuthDto.setPage(1);
        }
        //添加分页查询条件
        IPage page = new Page(newsAuthDto.getPage(), newsAuthDto.getSize());
        //新增查询条件
        LambdaQueryWrapper<WmNews> wlqw = new LambdaQueryWrapper<>();
        //校验有无指定id
        Integer wmNewsId = newsAuthDto.getWmNewsId();
        if (wmNewsId!=null){
            wlqw.eq(true,WmNews::getId,wmNewsId);
        }
//        //检验有无指定失败原因
//        String msg = newsAuthDto.getMsg();
//        if (StringUtils.isNotBlank(msg)){
//            wlqw.eq(true,WmNews::getReason,msg);
//        }
        //检验有无添加审核状态
        Integer status = newsAuthDto.getStatus();
        if (status!=null){
            wlqw.eq(true,WmNews::getStatus,status);
        }
        //校验有无添加文章标题
        String title = newsAuthDto.getTitle();
        if (StringUtils.isNotBlank(title)){
            wlqw.like(true,WmNews::getTitle,title);
        }
        //封装数据
        page = wmNewsMapper.selectPage(page, wlqw);
        ResponseResult pageRequestDto = new PageResponseResult(newsAuthDto.getPage(),newsAuthDto.getSize(), (int) page.getTotal());
        //通过查询出的userid查询自媒体作者姓名并封装
        List<WmNews> records = page.getRecords();
        //新建一个集合存放数据
        List<NewsAuthPageDto> newsAuthPageDtoList = new ArrayList<>();
        for (WmNews wmNews : records) {
            NewsAuthPageDto newsAuthPageDto = new NewsAuthPageDto();
            BeanUtils.copyProperties(wmNews,newsAuthPageDto);
            //根据用户id查询用户名
            WmUser wmUser = wmUserMapper.selectById(wmNews.getUserId());
            if (wmUser!=null){
                newsAuthPageDto.setUserName(wmUser.getName());
            }
            newsAuthPageDtoList.add(newsAuthPageDto);
        }
        pageRequestDto.setData(newsAuthPageDtoList);
        return pageRequestDto;
    }

    /**
     * 获取文章详情
     *
     * @param wmNewsId
     * @return
     */
    @Override
    public ResponseResult oneVo(Integer wmNewsId) {
        //检验数据
        if (wmNewsId==null){
            return  ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"缺少自媒体文章id");
        }
        //通过id获取自媒体文章数据
        WmNews wmNews = wmNewsMapper.selectById(wmNewsId);
        //校验数据
        if (wmNews==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"文章不存在");
        }
        //封装数据返回
        NewsAuthOneDto newsAuthOneDto = new NewsAuthOneDto();
        BeanUtils.copyProperties(wmNews,newsAuthOneDto);
        //根据作者id查询作者姓名
        WmUser wmUser = wmUserMapper.selectById(wmNews.getUserId());
        //校验数据
        if (wmUser==null||wmUser.getName()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST,"作者不存在");
        }
        //添加作者名
        newsAuthOneDto.setUserName(wmUser.getName());
        return ResponseResult.okResult(newsAuthOneDto);
    }

    /**
     * 审核文章 不通过
     *
     * @param newsAuthDto
     * @return
     */
    @Override
    public ResponseResult authFail(NewsAuthDto newsAuthDto) {
        //校验数据
        if (newsAuthDto==null||newsAuthDto.getWmNewsId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"自媒体文章id为空");
        }
        //通过id查询自媒体文章
        WmNews wmNews = wmNewsMapper.selectById(newsAuthDto.getWmNewsId());
        //校验数据
        if (wmNews==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"自媒体文章不存在");
        }
        //设置审核状态为不通过
        wmNews.setStatus(WmNews.Status.FAIL.getCode());
        //设置审核失败原因
        wmNews.setReason(newsAuthDto.getMsg());
        //修改数据库状态
        wmNewsMapper.updateById(wmNews);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 审核文章 通过
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult authPass(NewsAuthDto dto) {
        try {
            //校验数据
            if (dto==null||dto.getWmNewsId()==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"自媒体文章id为空");
            }
            //通过id查询自媒体文章
            WmNews wmNews = wmNewsMapper.selectById(dto.getWmNewsId());
            //校验数据
            if (wmNews==null){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE,"自媒体文章不存在");
            }
            //设置参数
            wmNews.setStatus(WmNews.Status.PUBLISHED.getCode());
            //设置审核成功
            wmNews.setReason("人工审核成功");
            WmNews wmNewsInfo = wmNewsAutoScanService.authPass(wmNews);
            //更新数据库
            wmNewsMapper.updateById(wmNewsInfo);
        } catch (Exception e) {
            throw new RuntimeException("人工审核通过失败");
        }
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
